From 327a8c06afe568c1bf017f80c2083255b02d320f Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Mon, 31 Aug 2020 21:13:38 -0500 Subject: [PATCH] Split into 3 modules: plugin fixtures, plugin helpers, testing functions --- setup.py | 2 +- src/pgwui_testing/pytest_plugin.py | 62 +++++++++++ src/pgwui_testing/pytest_plugin_helpers.py | 38 +++++++ src/pgwui_testing/testing.py | 53 +-------- tests/test_pytest_plugin.py | 108 ++++++++++++++++++ tests/test_pytest_plugin_helpers.py | 56 ++++++++++ tests/test_testing.py | 121 ++------------------- 7 files changed, 273 insertions(+), 167 deletions(-) create mode 100644 src/pgwui_testing/pytest_plugin.py create mode 100644 src/pgwui_testing/pytest_plugin_helpers.py create mode 100644 tests/test_pytest_plugin.py create mode 100644 tests/test_pytest_plugin_helpers.py diff --git a/setup.py b/setup.py index 9f66580..6a25f0a 100644 --- a/setup.py +++ b/setup.py @@ -163,6 +163,6 @@ setup( # ], # }, entry_points={ - 'pytest11': ['pgwui = pgwui_testing.testing'], + 'pytest11': ['pgwui = pgwui_testing.pytest_plugin'], }, ) diff --git a/src/pgwui_testing/pytest_plugin.py b/src/pgwui_testing/pytest_plugin.py new file mode 100644 index 0000000..025e55d --- /dev/null +++ b/src/pgwui_testing/pytest_plugin.py @@ -0,0 +1,62 @@ +# Copyright (C) 2015, 2018, 2020 The Meme Factory, Inc. +# http://www.karlpinc.com/ + +# This file is part of PGWUI_Testing. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +from pytest import ( + fixture, +) +from pyramid.testing import ( + DummyRequest, + setUp, + tearDown +) + +from pgwui_testing import pytest_plugin_helpers + + +# Fixtures + +@fixture +def pyramid_config(): + yield setUp() + tearDown() + + +@fixture +def pyramid_request_config(): + request = DummyRequest() + yield setUp(request=request) + tearDown() + + +@fixture +def pgwui_component_entry_point(): + '''Test that the supplied pgwui component is a pgui.components entry point + ''' + return pytest_plugin_helpers.pgwui_entry_point('pgwui.components') + + +@fixture +def pgwui_check_settings_entry_point(): + '''Test that the supplied pgwui component is a pgwui.check_settings + entry point + ''' + return pytest_plugin_helpers.pgwui_entry_point('pgwui.check_settings') diff --git a/src/pgwui_testing/pytest_plugin_helpers.py b/src/pgwui_testing/pytest_plugin_helpers.py new file mode 100644 index 0000000..da1cbf2 --- /dev/null +++ b/src/pgwui_testing/pytest_plugin_helpers.py @@ -0,0 +1,38 @@ +# Copyright (C) 2015, 2018, 2020 The Meme Factory, Inc. +# http://www.karlpinc.com/ + +# This file is part of PGWUI_Testing. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +'''Functions used by the pytest_plugin module. +''' + +import pkg_resources + + +def pgwui_entry_point(ep_name): + '''Test that the supplied pgwui component is a entry point with the + given name + ''' + def run(module): + return (module in + [entry_point.module_name for entry_point in + pkg_resources.iter_entry_points(ep_name)]) + + return run diff --git a/src/pgwui_testing/testing.py b/src/pgwui_testing/testing.py index 23cad3b..8b8c3e0 100644 --- a/src/pgwui_testing/testing.py +++ b/src/pgwui_testing/testing.py @@ -20,60 +20,9 @@ # Karl O. Pinc -import pkg_resources +from pytest import fixture from unittest import mock -from pytest import ( - fixture, -) -from pyramid.testing import ( - DummyRequest, - setUp, - tearDown -) - - -# Fixtures - -@fixture -def pyramid_config(): - yield setUp() - tearDown() - - -@fixture -def pyramid_request_config(): - request = DummyRequest() - yield setUp(request=request) - tearDown() - - -def pgwui_entry_point(ep_name): - '''Test that the supplied pgwui component is a entry point with the - given name - ''' - def run(module): - return (module in - [entry_point.module_name for entry_point in - pkg_resources.iter_entry_points(ep_name)]) - - return run - - -@fixture -def pgwui_component_entry_point(): - '''Test that the supplied pgwui component is a pgui.components entry point - ''' - return pgwui_entry_point('pgwui.components') - - -@fixture -def pgwui_check_settings_entry_point(): - '''Test that the supplied pgwui component is a pgwui.check_settings - entry point - ''' - return pgwui_entry_point('pgwui.check_settings') - # Mock support diff --git a/tests/test_pytest_plugin.py b/tests/test_pytest_plugin.py new file mode 100644 index 0000000..6f03215 --- /dev/null +++ b/tests/test_pytest_plugin.py @@ -0,0 +1,108 @@ +# Copyright (C) 2018, 2019, 2020 The Meme Factory, Inc. +# http://www.karlpinc.com/ + +# This file is part of PGWUI_Testing. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +# See: https://pytest-cov.readthedocs.io/en/latest/plugins.html + +# Allow use of the testdir fixture +pytest_plugins = ("pytester",) + + +# Test fixtures + + +# pgwui_component_entry_point() +# pgwui_check_settings_entry_point() + +def test_pgwui_testing_fixtures(testdir): + '''Test the fixtures supplied by the pytest_plugin module + ''' + testdir.makepyfile( + ''' + from pgwui_testing import pytest_plugin_helpers + import pytest + + from unittest import mock + + from pyramid.config import ( + Configurator + ) + from pyramid.threadlocal import ( + get_current_request + ) + + + # Activiate our pytest plugin + pytest_plugins = ("pgwui_testing",) + + # pyramid_config + + def test_pyramid_config(pyramid_config): + # A pyramid_config is a Configurator instance + result = pyramid_config + assert isinstance(result, Configurator) + + + # pyramid_test_config + + def test_pyramid_request_config(pyramid_request_config): + # Is a Configurator instance and has a non-None request + result = pyramid_request_config + assert isinstance(result, Configurator) + assert get_current_request() is not None + + + @pytest.fixture + def mock_pgwui_entry_point(monkeypatch): + mocked = mock.Mock( + spec=getattr(pytest_plugin_helpers, 'pgwui_entry_point'), + name='pgwui_entry_point') + monkeypatch.setattr(pytest_plugin_helpers, 'pgwui_entry_point', + mocked) + return mocked + + + # pgwui_component_entry_point + + def test_pgwui_component_entry_point( + mock_pgwui_entry_point, pgwui_component_entry_point): + #Calls pgwui_entry_point + + pgwui_component_entry_point('pgwui_example') + mock_pgwui_entry_point.assert_called_once() + + + # pgwui_check_settings_entry_point + + def test_pgwui_check_settings_entry_point( + mock_pgwui_entry_point, pgwui_check_settings_entry_point): + #Calls pgwui_entry_point + + pgwui_check_settings_entry_point('pgwui_example') + mock_pgwui_entry_point.assert_called_once() + + + ''' + ) + + result = testdir.runpytest() + + result.assert_outcomes(passed=4) diff --git a/tests/test_pytest_plugin_helpers.py b/tests/test_pytest_plugin_helpers.py new file mode 100644 index 0000000..09cb066 --- /dev/null +++ b/tests/test_pytest_plugin_helpers.py @@ -0,0 +1,56 @@ +# Copyright (C) 2020 The Meme Factory, Inc. +# http://www.karlpinc.com/ + +# This file is part of PGWUI_Testing. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +# See: https://pytest-cov.readthedocs.io/en/latest/plugins.html + +from pgwui_testing import pytest_plugin_helpers + + +# pgwui_entry_point() + +def test_pgwui_entry_point_there(monkeypatch): + '''True when the component is a pgwui.components entry point + ''' + test_name = 'pgwui_example' + + class MockEntryPoint(): + def __init__(self, module_name): + self.module_name = module_name + + monkeypatch.setattr( + pytest_plugin_helpers.pkg_resources, 'iter_entry_points', + lambda *args: [MockEntryPoint(test_name)]) + + assert ( + pytest_plugin_helpers.pgwui_entry_point(test_name)(test_name) + is True) + + +def test_pgwui_entry_point_not_there(monkeypatch): + '''False when the component is not pgwui.components entry point + ''' + monkeypatch.setattr( + pytest_plugin_helpers.pkg_resources, 'iter_entry_points', + lambda *args: []) + + assert (pytest_plugin_helpers.pgwui_entry_point('foo')('foo') + is False) diff --git a/tests/test_testing.py b/tests/test_testing.py index 500a54b..28c2c72 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -22,118 +22,11 @@ # See: https://pytest-cov.readthedocs.io/en/latest/plugins.html + import sys from pgwui_testing import testing -from pyramid.config import ( - Configurator -) -from pyramid.threadlocal import ( - get_current_request -) - - -# Activiate our pytest plugin -pytest_plugins = ("pgwui_testing", - "pytester") - -# Test fixtures - - -# pyramid_config - -def test_pyramid_config(pyramid_config): - # A pyramid_config is a Configurator instance - result = pyramid_config - assert isinstance(result, Configurator) - - -# pyramid_test_config - -def test_pyramid_request_config(pyramid_request_config): - # Is a Configurator instance and has a non-None request - result = pyramid_request_config - assert isinstance(result, Configurator) - assert get_current_request() is not None - - -# pgwui_entry_point() - -def test_pgwui_entry_point_there(monkeypatch): - # True when the component is a pgwui.components entry point - test_name = 'pgwui_example' - - class MockEntryPoint(): - def __init__(self, module_name): - self.module_name = module_name - - monkeypatch.setattr( - testing.pkg_resources, 'iter_entry_points', - lambda *args: [MockEntryPoint(test_name)]) - - assert testing.pgwui_entry_point(test_name)(test_name) is True - - -def test_pgwui_entry_point_not_there(monkeypatch): - # False when the component is not pgwui.components entry point - monkeypatch.setattr( - testing.pkg_resources, 'iter_entry_points', - lambda *args: []) - - assert testing.pgwui_entry_point('foo')('foo') is False - - -# pgwui_component_entry_point() -# pgwui_check_settings_entry_point() - -def test_pgwui_testing_fixtures(testdir): - '''Test the fixtures supplied by the testing module - ''' - testdir.makepyfile( - ''' - import pgwui_testing.testing as testing - import pytest - - from unittest import mock - - - @pytest.fixture - def mock_pgwui_entry_point(monkeypatch): - mocked = mock.Mock( - spec=getattr(testing, 'pgwui_entry_point'), - name='pgwui_entry_point') - monkeypatch.setattr(testing, 'pgwui_entry_point', mocked) - return mocked - - - # pgwui_component_entry_point - - def test_pgwui_component_entry_point( - mock_pgwui_entry_point, pgwui_component_entry_point): - #Calls pgwui_entry_point - - pgwui_component_entry_point('pgwui_example') - mock_pgwui_entry_point.assert_called_once() - - - # pgwui_check_settings_entry_point - - def test_pgwui_check_settings_entry_point( - mock_pgwui_entry_point, pgwui_check_settings_entry_point): - #Calls pgwui_entry_point - - pgwui_check_settings_entry_point('pgwui_example') - mock_pgwui_entry_point.assert_called_once() - - - ''' - ) - - result = testdir.runpytest() - - result.assert_outcomes(passed=2) - # Test functions @@ -151,8 +44,8 @@ mocked_func = testing.make_mock_fixture( def test_make_mock_fixture_fixture(mocked_func): - '''The mock of the function works - ''' + # The mock of the function works + test_value = 'test value' mocked_func.return_value = test_value result = mocked_func() @@ -178,8 +71,8 @@ mocked_method = testing.instance_method_mock_fixture('method_to_mock') def test_instance_method_mock_fixture(mocked_method): - '''The mock of the instance method works - ''' + # The mock of the instance method works + test_value = 'mocked value' cls = TestClass() mocked_method(cls).return_value = test_value @@ -190,8 +83,8 @@ def test_instance_method_mock_fixture(mocked_method): def test_instance_method_mock_fixture_unmocked(): - '''The test class works after the mocking - ''' + # The test class works after the mocking + cls = TestClass() result = cls.method_to_mock() -- 2.34.1